/*
 * SharedStatus.java
 *
 * Created on 20 Ноябрь 2006 г., 11:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.rost.mobile.mgtalk.model;

import java.util.Vector;
import org.rost.mobile.mgtalk.AppStore;
import org.rost.mobile.mgtalk.controllers.NetworkDispatcher;
import org.rost.mobile.mgtalk.ui.ContactListUI;
import org.rost.mobile.mgtalk.utils.NetworkTools;
import org.rost.mobile.mgtalk.utils.XmlNode;

/**
 *
 * @author Kostya
 */
public class SharedStatus {
    
    /** Creates a new instance of SharedStatus */
    protected Vector onlineList = new Vector();
    protected Vector awayList = new Vector();
    protected Vector busyList = new Vector();
    String status = "";
    protected boolean firstRefresh = true;
    public SharedStatus() {
	
    }
    void fillVector(XmlNode x, Vector v){
	v.removeAllElements();
	Vector childs = x.getChilds();
	for (int i = 0; i < childs.size(); i++) {
	    XmlNode n = (XmlNode) childs.elementAt(i);
	    v.addElement(n.getValue());
	}
    }
    
    public void refreshLists(XmlNode x){
	Vector childs = x.child("query").getChilds();
	status = x.child("query").childValue("status");
	if(!firstRefresh){
	    AppStore.getSelectedProfile().setStatus(x.child("query").childValue("status"));
	    AppStore.getSelectedProfile().setStatusID(ContactListUI.statusStringToNumber(x.child("query").childValue("show")));	    
	    AppStore.getSelectedProfile().setStatusActive(true);
	}
	firstRefresh = false;
	for (int i = 0; i < childs.size(); i++) {
	    XmlNode n = (XmlNode) childs.elementAt(i);
	    if(n.getName().equals("status-list")){
		if(n.getAttr("show").equals("away"))
		    fillVector(n, awayList);
		if(n.getAttr("show").equals("dnd"))
		    fillVector(n, busyList);
		if(n.getAttr("show").equals("default"))
		    fillVector(n, onlineList);
	    }
	}
    }
    
    public String statusListToString(Vector v){
	String s = "";
	for(int i=0; i<v.size(); i++){
	    s+="<status>"+v.elementAt(i)+"</status>";
	}
	return s;
    }
    
    public String getStatusStanza(){
	StringBuffer sb = new StringBuffer();
	if(AppStore.getSelectedProfile().isStatusActive())
	    status = AppStore.getSelectedProfile().getStatus();
	sb.append("<iq type=\"set\" to=\""+AppStore.getSelectedProfile().getUserName()+"\" id=\"ss-1\">" +
		"<query xmlns=\"google:shared-status\">" +
		"<status>"+NetworkTools.toXML(status)+"</status><show>"+
		NetworkDispatcher.statusIDtoString(AppStore.getSelectedProfile().getStatusID())+
		"</show>");
	sb.append("<status-list show=\"default\">"+statusListToString(onlineList)+"</status-list>");
	sb.append("<status-list show=\"away\">"+statusListToString(awayList)+"</status-list>");
	sb.append("<status-list show=\"dnd\">"+statusListToString(busyList)+"</status-list>");
	sb.append("</query></iq>");
	return sb.toString();
    }

	public boolean isFirstRefresh() {
		return firstRefresh;
	}

	public Vector getOnlineList() {
		return onlineList;
	}

	public Vector getAwayList() {
		return awayList;
	}

	public Vector getBusyList() {
		return busyList;
	}
        
        public void addToOnlineList(String status){
            if(!onlineList.contains(status))
                onlineList.insertElementAt(status, 0);
        }
        public void addToBusyList(String status){
            if(!busyList.contains(status))
                busyList.insertElementAt(status, 0);
        }
        public void addToAwayList(String status){
            if(!awayList.contains(status))
                awayList.insertElementAt(status, 0);
        }
}
